#include<iostream>
#include<vector>
#include<unordered_map>
using namespace std;
typedef long long ll;

ll count(vector<int>& a,int k,int l,int r){
    unordered_map<int,int>mp; 
    ll left=0,num=0,res=0;
    for (ll i=0;i<a.size();i++){
        if(mp[a[i]]==0){
            num++;
        }
        mp[a[i]]++;
        while(num>k){
            mp[a[left]]--;
            if (mp[a[left]]==0) {
                num--;
            }
            left++;
        }
        int mx=i-l+1;
        int mn=max(left,i-r+1LL);
        if (mx>=mn) {
            res+=mx-mn+1;
        }
    }
    return res;
}

void solve() {
    int n,k,l,r;
    cin>>n>>k>>l>>r;
    vector<int> a(n);
    for (int i=0;i<n;i++) {
        cin>>a[i];
    }
    ll res=count(a,k,l,r)-count(a,k-1,l,r);
    cout<<res<<'\n';
}

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0);
    int t=1;
    cin>>t;
    while(t--){
        solve();
    }
    return 0;
}